//
//  ShoppingCartCell.m
//  cm
//
//  Created by suyibaishan on 2017/10/30.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "ShoppingCartCell.h"

@implementation ShoppingCartCell
{
    UIView * bgView;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self constructUI];
    }
    return self;
}

-(void)constructUI
{
    //布局界面
    bgView = [[UIView alloc]initWithFrame:CGRectMake(5, 0, kScreenWidth-10, 95)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    
    //是否选中图片
    _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectBtn.frame = CGRectMake(5, 36, 23, 23);
    //        _isSelectImg.backgroundColor = [UIColor redColor];
    [_selectBtn addTarget:self action:@selector(selectBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_selectBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
    [_selectBtn setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
    [bgView addSubview:_selectBtn];
    
    //添加商品图片
    _goodsImg = [[UIImageView alloc] init];
    _goodsImg.layer.cornerRadius = 5;
//    _goodsImg.contentMode = UIViewContentModeScaleAspectFill;
//    _goodsImg.clipsToBounds = YES;
    _goodsImg.userInteractionEnabled = YES;
    [bgView addSubview:_goodsImg];
    _goodsImg.sd_layout
    .leftSpaceToView(_selectBtn, 8)
    .topSpaceToView(bgView, 10)
    .widthIs(75)
    .heightIs(75);
    UITapGestureRecognizer *goodsImgGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGoodsImg:)];
    [_goodsImg addGestureRecognizer:goodsImgGesture];
//    _goodsImg.backgroundColor = [UIColor redColor];
    
    //添加商品标题
    _goodsTitle = [[UILabel alloc] init];
    _goodsTitle.text = @"众泰汽车";
    //        _goodsTitle.backgroundColor = [UIColor grayColor];
    _goodsTitle.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:_goodsTitle];
    _goodsTitle.sd_layout
    .leftSpaceToView(_goodsImg, 5)
    .topSpaceToView(bgView, 8)
    .widthIs(kScreenWidth/2)
    .heightIs(20);
    
    _subtitleLabel = [[UILabel alloc]init];
    //        _normLabel.backgroundColor = [UIColor grayColor];
    _subtitleLabel.textColor = [UIColor colorWithRed:0.98 green:0.25 blue:0 alpha:1];
    _subtitleLabel.font = [UIFont systemFontOfSize:12];
    [bgView addSubview:_subtitleLabel];
    _subtitleLabel.sd_layout
    .leftSpaceToView(_goodsImg, 5)
    .topSpaceToView(_goodsTitle, 5)
    .widthIs(kScreenWidth - 135)
    .heightIs(15);
//    _subtitleLabel.text = @"指导价¥100000 首付价¥100000";
    
//    _normsLabel = [[UILabel alloc]init];
//    _normsLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
//    _normsLabel.font = [UIFont systemFontOfSize:12];
//    [bgView addSubview:_normsLabel];
//    _normsLabel.sd_layout
//    .topSpaceToView(_subheadLabel, 5)
//    .leftSpaceToView(_goodsImg, 5)
//    .widthIs(kScreenWidth - 210)
//    .heightIs(15);
//
//    _priceLab = [[UILabel alloc]init];
//    //        _priceLab.text = @"¥199.00";
//    _priceLab.textColor = [UIColor colorWithRed:0.95 green:0 blue:0 alpha:1];
//    _priceLab.font = [UIFont systemFontOfSize:14];
//    [bgView addSubview:_priceLab];
//    _priceLab.sd_layout
//    .topSpaceToView(_normsLabel, 5)
//    .leftSpaceToView(_goodsImg, 5)
//    .widthIs(kScreenWidth - 210)
//    .heightIs(15);
    
    UIView *numberView = [[UIView alloc] init];
    numberView.layer.borderWidth = 1;
    numberView.layer.borderColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1].CGColor;
    [bgView addSubview:numberView];
    numberView.sd_layout
    .leftSpaceToView(_goodsImg, 5)
    .bottomSpaceToView(bgView, 10)
    .widthIs(90)
    .heightIs(20);
    
    for (NSInteger i = 0; i < 2; i++) {
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
        [numberView addSubview:line];
        line.sd_layout
        .leftSpaceToView(numberView, 30+i*30)
        .topSpaceToView(numberView, 0)
        .widthIs(1)
        .heightIs(20);
    }
    
    //减按钮
    _reduceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_reduceBtn setImage:[UIImage imageNamed:@"reduce"] forState:UIControlStateNormal];
    //        _deleteBtn.layer.borderWidth = 0.5;
    //        _deleteBtn.layer.borderColor = [UIColor grayColor].CGColor;
    [_reduceBtn addTarget:self action:@selector(reduceBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _reduceBtn.tag = 1000;
    [numberView addSubview:_reduceBtn];
    _reduceBtn.sd_layout
    .leftSpaceToView(numberView, 0)
    .topSpaceToView(numberView, 0)
    .widthIs(30)
    .heightIs(20);
    
    //购买商品的数量
    _numCountLab = [[UILabel alloc]init];
    _numCountLab.textAlignment = NSTextAlignmentCenter;
    _numCountLab.font = [UIFont systemFontOfSize:14];
    //        _numCountLab.layer.borderWidth = 0.5;
    //        _numCountLab.layer.borderColor = [UIColor grayColor].CGColor;
    [numberView addSubview:_numCountLab];
    _numCountLab.sd_layout
    .leftSpaceToView(_reduceBtn, -0.5)
    .topSpaceToView(numberView, 0)
    .widthIs(30)
    .heightIs(20);
    
    //加按钮
    _plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_plusBtn setImage:[UIImage imageNamed:@"plus"] forState:UIControlStateNormal];
    //        _addBtn.layer.borderWidth = 0.5;
    //        _addBtn.layer.borderColor = [UIColor grayColor].CGColor;
    [_plusBtn addTarget:self action:@selector(plusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _plusBtn.tag = 1001;
    [numberView addSubview:_plusBtn];
    _plusBtn.sd_layout
    .leftSpaceToView(_numCountLab, -0.5)
    .topSpaceToView(numberView, 0)
    .widthIs(30)
    .heightIs(20);
    
    //删除按钮
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteBtn setImage:[UIImage imageNamed:@"cm_delete"] forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:deleteBtn];
    deleteBtn.sd_layout
    .rightSpaceToView(bgView, 5)
    .bottomSpaceToView(bgView, 10)
    .widthIs(19)
    .heightIs(22);
//    deleteBtn.backgroundColor = [UIColor redColor];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.89 alpha:1];
    [self.contentView addSubview:line];
    line.sd_layout
    .leftSpaceToView(self.contentView, 0)
    .bottomSpaceToView(self.contentView, 0)
    .widthIs(kScreenWidth)
    .heightIs(0.5);
}

-(void)tapGoodsImg:(UITapGestureRecognizer *)tap
{
    _tapAction();
}

-(void)selectBtnAction
{
    _selectAction();
}

-(void)deleteBtnAction
{
    _deleteAction();
}

/**
 *  给单元格赋值
 *
 *  @param model 里面存放各个控件需要的数值
 */
-(void)setModel:(ShoppingModel *)model
{
    _model = model;
    [_goodsImg sd_setImageWithURL:[NSURL URLWithString:[kIMGURL stringByAppendingString:model.imageName]] placeholderImage:nil];
    _goodsTitle.text = model.goodsTitle;
    _subtitleLabel.text = model.caption;
    _subtitleLabel.text = [NSString stringWithFormat:@"指导价¥%.2f  首付价¥%.2f",model.goodsPrice,model.downPayment];//@"指导价¥100000 首付价¥100000";
//    _normsLabel.text = [NSString stringWithFormat:@"%@",model.specificationValues];
//    _priceLab.text = [NSString stringWithFormat:@"¥%.2f",model.goodsPrice];
    _numCountLab.text = [NSString stringWithFormat:@"%ld",(long)model.goodsNum];
    if (model.selectState)
    {
        _selectBtn.selected = YES;
    }else{
        _selectBtn.selected = NO;
    }
}

/**
 *  点击减按钮实现数量的减少
 *
 *  @param sender 减按钮
 */
-(void)reduceBtnAction:(UIButton *)sender
{
    //调用代理
    [self.delegate btnClick:self andFlag:(NSInteger)sender.tag];
}
/**
 *  点击加按钮实现数量的增加
 *
 *  @param sender 加按钮
 */
-(void)plusBtnAction:(UIButton *)sender
{
    //调用代理
    [self.delegate btnClick:self andFlag:(NSInteger)sender.tag];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
